package com.cskaoyan.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.cskaoyan.dao.UserDao;
import com.cskaoyan.service.GoodsServiceImpl;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.sql.DataSource;

/**
 * @author stone
 * @date 2022/06/16 14:47
 */
@Configuration
@ComponentScan("com.cskaoyan")
@Import(GoodsServiceImpl.class)
public class SpringConfiguration {

    /*@Autowired
    UserService userService;*/

    /**
     * 返回值类型：Spring容器中的组件类型
     * 返回值：返回值注册为容器中的组件
     * 组件ID：默认值是方法名；如果想要指定组件id，可以使用@Bean的value属性
     */
    @Bean("druidDatasource")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/cskaoyan_db");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        return dataSource;
    }

    //@Autowired
    //@Qualifier("userDaoImpl")
    //UserDao userDao;

    /**
     * 形参：默认按照类型从容器中取出组件;想要指定组件id → @Qualifier
     */
    // 注册一个UserServiceImpl组件
    @Bean
    public UserService userService(@Qualifier("userDaoImpl") UserDao userDao) {
        //UserServiceImpl userService = new UserServiceImpl();
        // 从容器中取出userDao并且给其赋值
        //userService.setUserDao(userDao);
        UserServiceImpl userService = new UserServiceImpl(userDao);
        return userService;
    }
}
